package com.marui.ch1.vola;

/**
 * 演示volatile保证可见性
 * @author marui
 * @date 2020/4/9 9:28
 * @since 1.0
 */
public class VolatileDemo {
    //volatile int num = 0;
    int num = 0;
    public void setNumToSix(){
        num = 6;
    }

    public static void main(String[] args) {

        VolatileDemo volatileDemo = new VolatileDemo();

        new Thread(() -> {
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            volatileDemo.setNumToSix();
        }).start();

        while (volatileDemo.num == 0){

        }

        System.out.println("end and num is "+volatileDemo.num);

    }

}
